<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class AccountLog extends Model
{
    protected $table = 'account_log';
    public $timestamps = false;

    // 状态
    const STATUS_YES = 1; // 成功
    const STATUS_NO = 2; // 失败
    const STATUS_ING = 3; // 充值中

    //account_type 1八贝余额 2微信现金 3 八贝币 4 贝壳',
    const EIGHT_BALANCE = 1;
    const WX_PRICE = 2;
    const EIGHT_COIN = 3;
    const SHELL = 4;
    const ACCOUNT_TYPE = [
        self::EIGHT_BALANCE => "八贝余额",
        self::WX_PRICE => "微信现金",
        self::EIGHT_COIN => "八贝币",
        self::SHELL => "贝壳"
    ];
    const ACCOUNT_TYPE_VALUE = [
        'money_recharge'    => self::EIGHT_BALANCE,
        'money_eight_consh'    => self::EIGHT_COIN,
        'money_consh'    => self::SHELL,
    ];



    //  `type` tinyint(1) DEFAULT '0' COMMENT '变动类型（1充值 2消费 3退款）',
    const RECHARGE_TYPE = 1;
    const CONSUME_TYPE = 2;
    const REFUND_TYPE = 3;
    const ADMIN_RECHARGE_TYPE = 4;
    const TYPE = [
        self::RECHARGE_TYPE => "充值",
        self::CONSUME_TYPE => "消费",
        self::REFUND_TYPE => "退款",
        self::ADMIN_RECHARGE_TYPE => "后台充值"
    ];


    //`status` tinyint(1) DEFAULT '1' COMMENT '状态（1成功 2失败 3充值中）',
    const SUCCESS = 1;
    const FAIL = 2;
    const REFUND = 3;
    const ACCOUNT_LOG_STATUS = [
        self::SUCCESS => "成功",
        self::FAIL => "失败",
        self::REFUND => "充值中"
    ];

    const ACCOUNT_LOG_STATUS_SELECT = [
        self::SUCCESS => "成功",
        self::FAIL => "失败",
    ];

    public function userInfo(){
        return $this->hasOne('App\Models\User','id','user_id');
    }

    /**
     * 数据更新
     * @param $id
     * @param $data
     * @return mixed
     */
    public function edit($key, $value, $data)
    {
        if($this->findByWhere($key, $value)){
            return DB::table($this->table)->where($key, $value)->update($data);
        }
    }

    /**
     * 自定义多条件查询
     * @param $array
     * @return mixed
     */
    public function findByArray($array)
    {
        return DB::table($this->table)->where($array)->first();
    }

}
